package com.pw.manage.roster.service.impl;

import java.util.List;

import com.pw.manage.roster.domain.vo.EmployeesRostervo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.pw.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pw.manage.roster.domain.EmployeesExtraInfo;
import com.pw.manage.roster.mapper.EmployeesBaseInfoMapper;
import com.pw.domain.EmployeesBaseInfo;
import com.pw.manage.roster.service.IEmployeesBaseInfoService;

/**
 * 员工基本信息Service业务层处理
 *
 * @author lye
 * @date 2024-12-28
 */
@Service
public class EmployeesBaseInfoServiceImpl implements IEmployeesBaseInfoService
{
    @Autowired
    private EmployeesBaseInfoMapper employeesBaseInfoMapper;

    /**
     * 查询员工基本信息
     *
     * @param id 员工基本信息主键
     * @return 员工基本信息
     */
    @Override
    public EmployeesBaseInfo selectEmployeesBaseInfoById(Long id)
    {
        EmployeesBaseInfo employeesBaseInfo = employeesBaseInfoMapper.selectEmployeesBaseInfoById(id);
        return employeesBaseInfo;
    }

    /**
     * 查询员工基本信息列表
     *
     * @param employeesBaseInfo 员工基本信息
     * @return 员工基本信息
     */
    @Override
    public List<EmployeesBaseInfo> selectEmployeesBaseInfoList(EmployeesBaseInfo employeesBaseInfo)
    {
        return employeesBaseInfoMapper.selectEmployeesBaseInfoList(employeesBaseInfo);
    }

    /**
     * 新增员工基本信息
     *
     * @param employeesBaseInfo 员工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEmployeesBaseInfo(EmployeesBaseInfo employeesBaseInfo)
    {
        int rows = employeesBaseInfoMapper.insertEmployeesBaseInfo(employeesBaseInfo);
        insertEmployeesExtraInfo(employeesBaseInfo);
        return rows;
    }

    /**
     * 修改员工基本信息
     *
     * @param employeesBaseInfo 员工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEmployeesBaseInfo(EmployeesBaseInfo employeesBaseInfo)
    {
        employeesBaseInfoMapper.deleteEmployeesExtraInfoByEmployeeBaseInfoId(employeesBaseInfo.getId());
        insertEmployeesExtraInfo(employeesBaseInfo);
        return employeesBaseInfoMapper.updateEmployeesBaseInfo(employeesBaseInfo);
    }

    /**
     * 批量删除员工基本信息
     *
     * @param ids 需要删除的员工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeesBaseInfoByIds(Long[] ids)
    {
        employeesBaseInfoMapper.deleteEmployeesExtraInfoByEmployeeBaseInfoIds(ids);
        return employeesBaseInfoMapper.deleteEmployeesBaseInfoByIds(ids);
    }

    /**
     * 删除员工基本信息信息
     *
     * @param id 员工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeesBaseInfoById(Long id)
    {
        employeesBaseInfoMapper.deleteEmployeesExtraInfoByEmployeeBaseInfoId(id);
        return employeesBaseInfoMapper.deleteEmployeesBaseInfoById(id);
    }

    @Override
    public List<EmployeesRostervo> selectEmployeesRosterInfoList(EmployeesBaseInfo employeesBaseInfo) {
        return employeesBaseInfoMapper.selectEmployeesRosterInfoList(employeesBaseInfo);
    }

    /**
     * 新增员工额外信息模块信息
     *
     * @param employeesBaseInfo 员工基本信息对象
     */
    public void insertEmployeesExtraInfo(EmployeesBaseInfo employeesBaseInfo)
    {
        List<EmployeesExtraInfo> employeesExtraInfoList = employeesBaseInfo.getEmployeesExtraInfoList();
        Long id = employeesBaseInfo.getId();
        if (StringUtils.isNotNull(employeesExtraInfoList))
        {
            List<EmployeesExtraInfo> list = new ArrayList<EmployeesExtraInfo>();
            for (EmployeesExtraInfo employeesExtraInfo : employeesExtraInfoList)
            {
                employeesExtraInfo.setEmployeeBaseInfoId(id);
                list.add(employeesExtraInfo);
            }
            if (list.size() > 0)
            {
                employeesBaseInfoMapper.batchEmployeesExtraInfo(list);
            }
        }
    }
}
